home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / Book Demos in Pascal / TextGrid / TextGrid.p < prev    next >
Text File  |  1995-05-26  |  3KB  |  120 lines

  1. program TextGrid;
  2.  
  3. {$IFC UNDEFINED THINK_PASCAL}
  4.     uses
  5.         Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
  6.         OSUtils, ToolUtils, OSEvents, Resources;
  7. {$ENDC}
  8.  
  9.     const
  10. (*Size of the array*)
  11.         kArraySizeH = 15;
  12.         kArraySizeV = 12;
  13.  
  14. (*Size of the tiles*)
  15.         kTileSizeH = 32;
  16.         kTileSizeV = 32;
  17.  
  18.     var
  19. (*The world is accessed through a Handle to a 'TEXT' resource:*)
  20.         gWorldHandle: Handle;    {Not a "GWorld", but a Global Handle to the world!}
  21.  
  22.     const
  23. (*…and this is the resource number:*)
  24.         kWorldResource = 128;
  25.  
  26.     var
  27. (* Pictures*)
  28.         floorTile: PicHandle;
  29.         playerTile: PicHandle;
  30.         enemyTile: PicHandle;
  31.         goldTile: PicHandle;
  32.         wallTile: PicHandle;
  33.         exitTile: PicHandle;
  34.  
  35. (* Draw a tile *)
  36.  
  37.     procedure DrawTile (h: Integer; v: Integer);
  38.         var
  39.             tileRectangle: Rect;
  40.             theTile: char;
  41.     begin
  42.         SetRect(tileRectangle, h * kTileSizeH, v * kTileSizeV, (h + 1) * kTileSizeH, (v + 1) * kTileSizeV);
  43.  
  44.         theTile := Char(Ptr(Longint(gWorldHandle^) + h + v * (kArraySizeH + 1))^);
  45.  
  46.         case theTile of
  47.             ' ': 
  48.                 DrawPicture(floorTile, tileRectangle);
  49.             '#': 
  50.                 DrawPicture(wallTile, tileRectangle);
  51.             '@': 
  52.                 DrawPicture(playerTile, tileRectangle);
  53.             '*': 
  54.                 DrawPicture(enemyTile, tileRectangle);
  55.             '$': 
  56.                 DrawPicture(goldTile, tileRectangle);
  57.             'X': 
  58.                 DrawPicture(exitTile, tileRectangle);
  59.             otherwise
  60.                 PaintRect(tileRectangle);
  61.         end; {case}
  62.     end; (*DrawTile*)
  63.  
  64.  
  65. (* Standard inits *)
  66.  
  67.     procedure InitToolbox;
  68.     begin
  69. {$IFC UNDEFINED THINK_PASCAL}
  70.         InitGraf(@qd.thePort);
  71.         InitFonts;
  72.         FlushEvents(everyEvent, 0);
  73.         InitWindows;
  74.         InitMenus;
  75.         TEInit;
  76.         InitDialogs(nil);
  77. {$ENDC}
  78.         InitCursor;
  79.     end;
  80.  
  81.  
  82. (****************** Main program ******************)
  83.  
  84.     var
  85.         myWindow: WindowPtr;
  86.         windowRectangle: Rect;
  87.         h, v: Integer;
  88.  
  89. begin
  90.     InitToolbox;
  91.  
  92. (*Set up the window*)
  93.     SetRect(windowRectangle, 50, 50, 50 + kArraySizeH * kTileSizeH, 50 + kArraySizeV * kTileSizeV);
  94.     myWindow := NewCWindow(nil, windowRectangle, 'Text grid demo', true, 0, WindowPtr(-1), false, 0);
  95.     SetPort(myWindow);
  96.  
  97. (*Load all pictures*)
  98.     floorTile := GetPicture(128);            (*PICT resource #128.*)
  99.     playerTile := GetPicture(129);            (*PICT resource #129.*)
  100.     enemyTile := GetPicture(130);            (*PICT resource #130.*)
  101.     goldTile := GetPicture(131);                (*PICT resource #131.*)
  102.     wallTile := GetPicture(132);                (*PICT resource #132.*)
  103.     exitTile := GetPicture(133);                (*PICT resource #133.*)
  104.  
  105. (*Get the resource*)
  106.     gWorldHandle := GetResource('TEXT', kWorldResource);
  107.     if (gWorldHandle = nil) then    (*Error! In a real program, you should check the picts above as well.*)
  108.         begin
  109.             SysBeep(1);
  110.             ExitToShell;
  111.         end;
  112.  
  113. (*Draw all tiles!*)
  114.     for h := 0 to kArraySizeH - 1 do
  115.         for v := 0 to kArraySizeV - 1 do
  116.             DrawTile(h, v);
  117.  
  118.     while not Button do
  119.         ;
  120. end. (*TextGrid*)